Hibernate & Lazy/Eager Loading - Questions and Answers

Audio 1 - Audio 2

Set 1

1. What is Hibernate and how does it relate to Lazy and Eager Loading?
Hibernate is an ORM (Object-Relational Mapping) framework that facilitates the conversion between Java objects and relational database tables. Lazy and Eager Loading are strategies used to control the fetching of related entities in Hibernate.
2. What is Lazy Loading in Hibernate?
Lazy Loading in Hibernate means that related entities are fetched only when they are accessed for the first time. This improves performance by avoiding unnecessary database queries.
3. What is Eager Loading in Hibernate?
Eager Loading in Hibernate means that related entities are fetched immediately when the parent entity is loaded. This may lead to multiple database queries but ensures that related data is available.
4. How do you implement Lazy Loading in Hibernate?
Lazy Loading is implemented using the `@OneToMany(fetch = FetchType.LAZY)` annotation on the relationship, meaning the related entities are fetched only when explicitly accessed.
@Entity public class ParentEntity { @OneToMany(fetch = FetchType.LAZY) private Set children; }
5. How do you implement Eager Loading in Hibernate?
Eager Loading is implemented using the `@OneToMany(fetch = FetchType.EAGER)` annotation, causing related entities to be loaded immediately when the parent entity is fetched.
@Entity public class ParentEntity { @OneToMany(fetch = FetchType.EAGER) private Set children; }
6. What are the pros and cons of Lazy Loading?
Pros of Lazy Loading: Cons of Lazy Loading:
7. What are the pros and cons of Eager Loading?
Pros of Eager Loading: Cons of Eager Loading:
8. What is the "N+1 select problem" in Hibernate?
The "N+1 select problem" occurs when Hibernate performs one query to load the parent entity and one query per related entity. This can result in many unnecessary database queries and performance issues.
9. How can you prevent the "N+1 select problem" in Hibernate?
The "N+1 select problem" can be avoided by using Eager Loading (`FetchType.EAGER`), or more commonly by using `@ManyToOne(fetch = FetchType.LAZY)` and then using `JOIN FETCH` in the query.
@Query("SELECT p FROM ParentEntity p JOIN FETCH p.children") List findAllWithChildren();
10. What is the `@Fetch` annotation in Hibernate, and how does it help with Lazy/Eager loading?
The `@Fetch` annotation allows you to define how associations are fetched, for example, `FetchMode.JOIN` can be used to fetch associations eagerly in a single query, while `FetchMode.SELECT` can be used to fetch them lazily.
@Entity public class ParentEntity { @OneToMany(fetch = FetchType.LAZY) @Fetch(FetchMode.JOIN) private Set children; }

Set 2

11. How does Lazy Loading affect performance in Hibernate applications?
Lazy Loading can improve performance by loading related entities only when accessed, reducing the number of database queries and the amount of data fetched. However, it can lead to performance issues if entities are accessed multiple times.
12. Can you change the default fetching strategy in Hibernate?
Yes, the fetching strategy can be changed using annotations like `@ManyToOne(fetch = FetchType.LAZY)` for Lazy Loading or `@OneToMany(fetch = FetchType.EAGER)` for Eager Loading.
13. How does Hibernate handle the session when using Lazy Loading?
Hibernate keeps the session open while lazily loading data. If the session is closed before accessing the lazy-loaded entity, a `LazyInitializationException` occurs.
14. How can you solve the `LazyInitializationException` in Hibernate?
You can solve the `LazyInitializationException` by ensuring that the session remains open when accessing lazy-loaded entities or by using `@Transactional` to manage session handling in Spring.
@Transactional public void fetchData() { ParentEntity parent = parentRepository.findById(1L); Set children = parent.getChildren(); // No LazyInitializationException }
15. What is the effect of `FetchType.LAZY` on relationships in Hibernate?
`FetchType.LAZY` delays the loading of related entities until they are explicitly accessed. It helps to avoid unnecessary database queries but can result in additional queries when the related entities are accessed.
16. What is the default fetch type for `@ManyToOne` relationships in Hibernate?
The default fetch type for `@ManyToOne` relationships in Hibernate is `FetchType.EAGER`, meaning that related entities are fetched immediately with the parent entity.
17. What is the `FetchMode` in Hibernate and how does it work with Lazy/Eager loading?
`FetchMode` in Hibernate defines how associated entities are fetched. `FetchMode.JOIN` fetches the related entity eagerly in a single query, while `FetchMode.SELECT` uses separate queries for each related entity (Lazy Loading).
18. How does Hibernate decide which fetch strategy to use by default?
Hibernate decides the default fetch strategy based on the annotation or mapping configuration. For example, `@ManyToOne` defaults to `FetchType.EAGER`, and `@OneToMany` defaults to `FetchType.LAZY`.
19. How can you optimize performance when using Lazy Loading with Hibernate?
Performance can be optimized by minimizing the number of queries (e.g., using `JOIN FETCH`), eagerly loading only the necessary entities, and using batch fetching strategies to load multiple entities at once.
@Query("SELECT p FROM ParentEntity p JOIN FETCH p.children") List findAllWithChildren();
20. How does Hibernate handle the fetching of collections in Lazy Loading?
In Lazy Loading, Hibernate fetches collections (e.g., `List`, `Set`) when they are accessed for the first time. If not accessed, the collection will not be loaded, which can help reduce memory usage.

Set 3

21. How can you specify Eager Loading in Hibernate?
Eager Loading can be specified using `@ManyToOne(fetch = FetchType.EAGER)` or `@OneToMany(fetch = FetchType.EAGER)` annotations, causing related entities to be fetched immediately.
22. What is the default fetching strategy for `@OneToMany` in Hibernate?
The default fetching strategy for `@OneToMany` relationships in Hibernate is `FetchType.LAZY`, meaning the associated entities will not be fetched unless explicitly accessed.
23. How can you override the default fetch type in Hibernate?
You can override the default fetch type by using the `fetch` attribute in the annotation. For example, `@OneToMany(fetch = FetchType.EAGER)` to eagerly fetch related entities.
24. What is the difference between Lazy Loading and Eager Loading in terms of database queries?
Lazy Loading results in fewer database queries since related entities are loaded only when accessed, whereas Eager Loading fetches all related entities in a single query, which might increase the number of queries.
25. How do you handle LazyInitializationException in Hibernate?
LazyInitializationException occurs when you try to access a lazily loaded entity outside the scope of the session. You can solve this by ensuring the session is open or by using `@Transactional`.
26. Can Lazy Loading be used with collections in Hibernate?
Yes, Lazy Loading can be used with collections (e.g., `@OneToMany`). The collection is loaded only when it is accessed for the first time.
27. What is `JOIN FETCH` in Hibernate and how does it relate to Lazy Loading?
`JOIN FETCH` is used in Hibernate queries to eagerly load associated entities in a single query. It overrides Lazy Loading and fetches related entities immediately, reducing the number of queries.
@Query("SELECT p FROM ParentEntity p JOIN FETCH p.children") List findAllWithChildren();
28. What is the impact of using Lazy Loading on performance?
Lazy Loading can improve performance by reducing unnecessary database queries, but it may cause additional queries if related entities are frequently accessed after the initial loading.
29. How can you change the fetch type for `@OneToMany` relationships in Hibernate?
You can change the fetch type for `@OneToMany` by specifying the `fetch` attribute in the annotation: `@OneToMany(fetch = FetchType.EAGER)` or `@OneToMany(fetch = FetchType.LAZY)`.
30. How can you prevent Lazy Loading from causing N+1 query problems in Hibernate?
You can prevent N+1 query problems by using `JOIN FETCH` in HQL or JPQL to fetch related entities in a single query. Additionally, batch fetching can be enabled to fetch multiple entities at once.

Set 4

31. What is the purpose of `@Transactional` in Hibernate when dealing with Lazy Loading?
`@Transactional` ensures that the session is open during the transaction, which allows Lazy Loading to fetch related entities without causing a `LazyInitializationException` after the session is closed.
32. How can you fetch data eagerly using a query in Hibernate?
You can fetch data eagerly by using a `JOIN FETCH` clause in your HQL or JPQL query to load related entities immediately.
@Query("SELECT p FROM ParentEntity p JOIN FETCH p.children") List findAllWithChildren();
33. What is the effect of `FetchType.LAZY` on `@ManyToOne` relationships?
`FetchType.LAZY` on `@ManyToOne` means that the related entity will not be fetched when the parent entity is loaded. It will only be fetched when explicitly accessed.
34. Can you force Eager Loading for collections in Hibernate?
Yes, you can force Eager Loading for collections by using `@OneToMany(fetch = FetchType.EAGER)` or by using `JOIN FETCH` in your queries.
35. What is the `LazyCollection` annotation used for in Hibernate?
The `LazyCollection` annotation is used to specify how collections are fetched. It is used with `@OneToMany` and `@ManyToMany` to indicate whether the collection should be fetched lazily or eagerly.
36. How does Hibernate optimize Lazy Loading with `batch-fetching`?
Batch-fetching in Hibernate allows you to load related entities in batches instead of one by one, thus reducing the number of SQL queries when accessing multiple related entities.
37. How can you configure batch-fetching in Hibernate?
Batch-fetching can be configured in the `hibernate.cfg.xml` file or in annotations by setting `hibernate.jdbc.batch_size` property.
hibernate.jdbc.batch_size=10
38. What are the drawbacks of using Eager Loading in Hibernate?
Eager Loading can lead to performance issues by loading unnecessary data, especially in large datasets. It may cause a high memory load or increased database query times.
39. What is the difference between `FetchType.EAGER` and `FetchType.LAZY` in terms of query execution?
`FetchType.EAGER` causes the related entities to be fetched in the same query as the parent entity, whereas `FetchType.LAZY` only loads related entities when accessed, potentially requiring additional queries.
40. Can Hibernate automatically choose between Lazy and Eager loading based on usage patterns?
No, Hibernate does not automatically choose between Lazy and Eager loading. The fetch strategy needs to be specified explicitly using annotations or configurations.

Set 5

41. What happens if you try to access a lazily-loaded entity outside the session in Hibernate?
If you try to access a lazily-loaded entity outside of the session, a `LazyInitializationException` is thrown because the session is closed and the entity cannot be fetched.
42. What is the impact of using `FetchType.LAZY` on performance?
`FetchType.LAZY` can improve performance by delaying the loading of related entities until they are actually needed. However, it can cause performance issues if entities are accessed frequently, resulting in multiple database queries.
43. How does Hibernate manage the session when using Lazy Loading?
Hibernate uses the session to fetch lazily-loaded entities. As long as the session is open, lazy-loaded associations can be fetched. If the session is closed, accessing lazy-loaded entities will trigger a `LazyInitializationException`.
44. What is `FetchType.EAGER` and how does it affect performance?
`FetchType.EAGER` causes Hibernate to fetch related entities immediately with the parent entity. While this can reduce the number of queries, it may negatively impact performance by loading unnecessary data.
45. What is the difference between eager and lazy fetching in terms of the SQL query generated?
In eager fetching, a `JOIN` is used in the SQL query to load both the parent and related entities in a single query. In lazy fetching, separate SQL queries are executed for the parent entity and its related entities when accessed.
46. How can you prevent LazyInitializationException in Hibernate when using Lazy Loading?
You can prevent `LazyInitializationException` by ensuring that the session remains open when accessing lazily-loaded entities or by fetching related entities eagerly using `JOIN FETCH`.
47. How can you use `@Fetch` in Hibernate to control the loading behavior of collections?
`@Fetch` is used to specify the fetching strategy for collections, such as `@Fetch(FetchMode.SELECT)` or `@Fetch(FetchMode.JOIN)`, to control how related entities are loaded.
48. How does Hibernate manage the fetch strategy for `@OneToMany` associations?
By default, `@OneToMany` relationships use `FetchType.LAZY`, but the fetch strategy can be changed to `FetchType.EAGER` or controlled using query-based `JOIN FETCH` to load related entities.
49. What is the effect of `@ManyToOne(fetch = FetchType.LAZY)` on database queries?
`@ManyToOne(fetch = FetchType.LAZY)` means that the associated entity is not loaded immediately with the parent entity, and a separate query will be executed when the relationship is accessed for the first time.
50. How does Hibernate handle the fetch strategy for `@ManyToMany` relationships?
Hibernate can use either `FetchType.LAZY` or `FetchType.EAGER` for `@ManyToMany` relationships. By default, it uses lazy loading, but you can specify eager loading using annotations or queries.

Set 6

51. How can you configure fetch type for relationships in Hibernate via XML configuration?
In XML configuration, you can set the fetch type using the `fetch` attribute in the ``, ``, ``, and `` elements, like this: ``.
52. What are the potential downsides of using `FetchType.EAGER` for large collections?
Using `FetchType.EAGER` for large collections can result in performance issues due to loading a large number of related entities in a single query, leading to increased memory usage and slower response times.
53. How does Hibernate handle fetching strategies for unidirectional associations?
Hibernate handles fetching strategies for unidirectional associations in the same way as bidirectional associations. You can specify the fetch type using annotations (`@OneToMany(fetch = FetchType.LAZY)`) or queries.
54. How can you use the `@BatchSize` annotation to optimize Lazy Loading in Hibernate?
The `@BatchSize` annotation can be used to control the number of entities fetched in one query during Lazy Loading. It can reduce the number of queries by fetching entities in batches instead of individually.
55. What is the impact of Lazy Loading when working with a large dataset in Hibernate?
Lazy Loading can improve performance by loading only the required entities when accessed. However, if many related entities are accessed frequently, it can result in multiple queries, which may degrade performance for large datasets.
56. What does `hibernate.enable_lazy_load_no_trans` configuration option do in Hibernate?
The `hibernate.enable_lazy_load_no_trans` option allows lazy loading of associations even when there is no active Hibernate transaction. This can help avoid `LazyInitializationException` when accessing lazily-loaded entities outside a transaction.
57. How do you configure fetch type using the `@ManyToMany` annotation?
You can configure the fetch type using the `fetch` attribute in the `@ManyToMany` annotation, like this: `@ManyToMany(fetch = FetchType.LAZY)`.
58. What is the relationship between Lazy Loading and the Open Session in View pattern in Hibernate?
The Open Session in View pattern ensures that the Hibernate session is kept open during the entire request, which allows Lazy Loading to work properly even after the initial entity is loaded and the transaction is closed.
59. How can you handle Lazy Loading when using `@OneToOne` associations in Hibernate?
For `@OneToOne` associations, you can specify the fetch type as `FetchType.LAZY` or `FetchType.EAGER`. By default, `@OneToOne` uses `FetchType.EAGER`, but you can change this using annotations or in queries.
60. What is the significance of `@ManyToOne(fetch = FetchType.LAZY)` in terms of query execution?
`@ManyToOne(fetch = FetchType.LAZY)` means that the related entity will not be fetched initially with the parent entity. Instead, a separate query will be executed when the associated entity is accessed.

Set 7

61. How does `@OneToMany(fetch = FetchType.LAZY)` work in Hibernate when accessing the related collection?
With `FetchType.LAZY`, the related collection is not fetched immediately. A separate query is issued only when the collection is accessed for the first time within an active session.
62. What is the difference between `@ManyToOne` and `@OneToMany` in terms of Lazy/Eager fetching?
`@ManyToOne` typically uses `FetchType.LAZY` by default, while `@OneToMany` defaults to `FetchType.LAZY` but can be configured to `EAGER`. In `@ManyToOne`, the associated entity is fetched lazily when accessed, while `@OneToMany` may trigger additional queries to fetch collections.
63. How do you fetch collections lazily in Hibernate when using the `@ManyToMany` annotation?
You can specify lazy loading for `@ManyToMany` by using `fetch = FetchType.LAZY`, which will load the related entities only when accessed, without fetching them immediately during the initial query.
64. What are some strategies to avoid the N+1 problem in Hibernate when using Lazy Loading?
Strategies to avoid the N+1 problem include using `JOIN FETCH` in HQL or JPQL queries to load related entities in a single query, or configuring batch fetching using `@BatchSize` to fetch multiple entities at once.
65. What is the `@Fetch` annotation used for in Hibernate?
The `@Fetch` annotation controls how Hibernate fetches a collection or association. It allows you to specify whether to use `FetchMode.JOIN` or `FetchMode.SELECT`, thus impacting the strategy for loading the related entities.
66. What does the `hibernate.default_batch_fetch_size` configuration property do in Hibernate?
The `hibernate.default_batch_fetch_size` property specifies the batch size for fetching entities when batch-fetching is enabled. It controls the number of entities that are loaded in each query during Lazy Loading.
67. How can you configure eager loading for all `@OneToMany` associations globally in Hibernate?
You can globally configure eager loading for `@OneToMany` associations by using the `hibernate.fetch_size` property in the configuration file or by setting `FetchType.EAGER` on each `@OneToMany` annotation.
68. What is the effect of enabling `hibernate.use_outer_join` in Hibernate configuration?
Enabling `hibernate.use_outer_join` forces Hibernate to use outer joins instead of inner joins for fetching associated entities. This can help in certain cases with `FetchType.LAZY`, as it ensures all entities are loaded in one query, even with missing associations.
69. Can you use a custom fetch strategy in Hibernate? How?
Yes, you can use a custom fetch strategy in Hibernate by overriding the default fetch type with `@Fetch` or by using `JOIN FETCH` in HQL/JPQL queries to customize how related entities are fetched.
70. What is the potential downside of using Lazy Loading in a web application?
The potential downside of Lazy Loading in a web application is that it can result in multiple database queries if related entities are accessed frequently. This may degrade performance due to excessive round trips to the database.

Set 8

71. What is the purpose of the `@LazyCollection` annotation in Hibernate?
The `@LazyCollection` annotation in Hibernate is used to configure the fetch strategy for collections. It allows you to specify whether the collection should be loaded lazily or eagerly, overriding the default behavior set by `FetchType`.
72. How can you prevent LazyInitializationException in Hibernate when accessing a lazy-loaded collection outside a session?
To prevent `LazyInitializationException`, you can either fetch the collection eagerly, keep the session open when accessing the collection, or initialize the collection explicitly before closing the session using methods like `Hibernate.initialize()`.
73. How can you use Hibernate’s `@Query` annotation to optimize fetching strategies?
The `@Query` annotation allows you to write custom queries (HQL or JPQL) and use `JOIN FETCH` to optimize fetching strategies. This enables eager fetching for specific associations within the query itself.
74. What is the role of `FetchMode` in Hibernate's `@Fetch` annotation?
The `FetchMode` in the `@Fetch` annotation controls how the related entities are fetched. It can be set to `FetchMode.JOIN` (using SQL JOINs to fetch the related entities) or `FetchMode.SELECT` (performing separate queries for related entities).
75. How do you use the `@OneToOne(fetch = FetchType.LAZY)` annotation in Hibernate, and what are its implications?
The `@OneToOne(fetch = FetchType.LAZY)` annotation tells Hibernate to lazily load the associated entity, meaning the related entity will only be fetched when it is accessed for the first time within an active session. This helps optimize performance by avoiding unnecessary data loading.
76. How can Hibernate’s `@ManyToOne(fetch = FetchType.LAZY)` be used with a collection of entities in a parent-child relationship?
When using `@ManyToOne(fetch = FetchType.LAZY)`, the child entity is loaded lazily. If the parent entity has a collection of such child entities, you can configure the collection fetch type separately using `FetchType.LAZY` or `FetchType.EAGER`.
77. What is the impact of using Lazy Loading with a large number of records in a database?
Lazy Loading can result in multiple database queries, each one fetching related records. This can lead to performance degradation when dealing with a large number of records, causing excessive database round trips and increased memory usage.
78. How does Hibernate handle fetching strategies in a polymorphic association (e.g., inheritance)?
Hibernate handles fetching strategies in polymorphic associations by using the fetch type defined in the association mapping. You can specify lazy or eager loading for the parent or child entities, and Hibernate will load the related entities accordingly.
79. What is the difference between lazy loading and batch fetching in Hibernate?
Lazy loading loads associations on demand when accessed, while batch fetching allows Hibernate to load multiple entities at once in one query. Batch fetching can help optimize performance by reducing the number of queries issued for related entities.
80. Can you use `@ManyToOne(fetch = FetchType.LAZY)` and `@OneToMany(fetch = FetchType.EAGER)` together in Hibernate? What would be the result?
Yes, you can use `@ManyToOne(fetch = FetchType.LAZY)` and `@OneToMany(fetch = FetchType.EAGER)` together. This setup means the `@ManyToOne` relationship will be lazily loaded, while the `@OneToMany` collection will be eagerly loaded when accessed.

Set 9

81. How does `@Fetch(FetchMode.SUBSELECT)` work in Hibernate?
`@Fetch(FetchMode.SUBSELECT)` allows Hibernate to fetch collections in a batch using a single SQL query for all entities, as opposed to executing separate queries for each collection. This approach can reduce the number of queries when dealing with multiple collections.
82. How can Hibernate’s `@ManyToOne` annotation be configured to load associated entities lazily?
By setting `fetch = FetchType.LAZY` in the `@ManyToOne` annotation, you instruct Hibernate to load the associated entity lazily. The entity will only be fetched when accessed, rather than immediately upon the parent entity’s retrieval.
83. How can you enable Lazy Loading for a `@ManyToMany` relationship in Hibernate?
You can enable Lazy Loading for a `@ManyToMany` relationship by setting `fetch = FetchType.LAZY` in the `@ManyToMany` annotation. This will cause the related entities to be fetched lazily when accessed within an active session.
84. How does Hibernate’s `@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)` behave?
With `@OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)`, Hibernate eagerly loads the collection of associated entities. The related entities are loaded immediately, together with the parent entity, in a single query.
85. How can you handle the LazyInitializationException in Hibernate when working with a detached entity?
To handle `LazyInitializationException`, you can either reattach the entity to a session using `Session.merge()` or `Session.update()`, or use `Hibernate.initialize()` to initialize the lazy-loaded associations before the session is closed.
86. What is the impact of using `FetchType.LAZY` on performance in a Hibernate application?
Using `FetchType.LAZY` improves performance by loading only the required associations when accessed, rather than eagerly loading all related entities. This reduces the amount of data fetched initially and prevents unnecessary database queries.
87. What does the `@OneToOne(fetch = FetchType.LAZY)` annotation do in terms of fetching behavior?
The `@OneToOne(fetch = FetchType.LAZY)` annotation tells Hibernate to lazily load the associated entity. The related entity will not be fetched when the parent entity is retrieved, but instead, only when the association is accessed within an active session.
88. How can Hibernate be configured to use a custom fetch strategy for a specific association?
Hibernate can be configured to use a custom fetch strategy by using `@Fetch(FetchMode)` to specify either `JOIN` or `SELECT` fetching, or by writing custom HQL/JPQL queries with `JOIN FETCH` for specific associations.
89. What is the difference between `@ManyToOne(fetch = FetchType.EAGER)` and `@ManyToOne(fetch = FetchType.LAZY)`?
`@ManyToOne(fetch = FetchType.EAGER)` fetches the associated entity immediately along with the parent entity, while `@ManyToOne(fetch = FetchType.LAZY)` defers the fetching of the associated entity until it is accessed, improving performance in certain scenarios.
90. What is the purpose of using `JOIN FETCH` in Hibernate queries?
`JOIN FETCH` is used in Hibernate queries (HQL or JPQL) to force eager fetching of associations. This can help avoid the N+1 problem by loading associated entities in a single query, rather than issuing separate queries for each association.

Set 10

91. How can you use the `@ManyToOne(fetch = FetchType.EAGER)` annotation to load a related entity eagerly in Hibernate?
Using `@ManyToOne(fetch = FetchType.EAGER)` tells Hibernate to eagerly load the associated entity when the parent entity is loaded. This means that both the parent and the related entity will be fetched in the same query, reducing the number of database round trips.
92. What is the difference between `@ManyToOne` and `@OneToMany` associations in terms of fetch strategies?
In a `@ManyToOne` association, the associated entity is often loaded eagerly or lazily based on the fetch type configuration. In contrast, `@OneToMany` associations typically use lazy loading by default, unless explicitly set to eager loading, due to the potential overhead of loading large collections.
93. How do you use `@ManyToOne` with `fetch = FetchType.LAZY` to improve performance?
By using `fetch = FetchType.LAZY`, you instruct Hibernate to defer the loading of the associated entity until it is actually accessed. This helps improve performance by reducing the amount of data fetched when loading the parent entity, particularly when the associated entity may not be needed.
94. How can Hibernate’s `@OneToOne(fetch = FetchType.LAZY)` annotation affect your application's performance?
The `@OneToOne(fetch = FetchType.LAZY)` annotation can improve performance by only loading the associated entity when it is accessed. This helps avoid unnecessary database queries for related entities that may not be needed immediately.
95. Can Hibernate’s Lazy Loading be configured globally, and if so, how?
Yes, Hibernate’s Lazy Loading can be configured globally by setting the default fetch type to lazy in the Hibernate configuration file (`hibernate.cfg.xml`) or by setting properties in your persistence context. This will apply lazy loading to all associations unless overridden by specific annotations or queries.
96. How does Hibernate handle Lazy Loading in a `@OneToMany` relationship when using `FetchType.LAZY`?
In a `@OneToMany` relationship, when `FetchType.LAZY` is used, Hibernate does not load the associated collection immediately when the parent entity is loaded. Instead, the collection is only loaded when it is accessed within an active session, reducing unnecessary data loading.
97. What are the possible issues you might encounter when using Lazy Loading in Hibernate with large datasets?
Issues that can arise with Lazy Loading include the N+1 query problem (multiple queries being executed for each lazy-loaded association), performance degradation due to excessive queries, and potential `LazyInitializationException` when the session is closed before accessing the lazy-loaded association.
98. How can you prevent Hibernate from issuing multiple queries in a Lazy Loaded collection?
To prevent multiple queries, you can use strategies like `@Fetch(FetchMode.JOIN)` or `@Fetch(FetchMode.SUBSELECT)` to optimize fetching. These strategies ensure that the collection is fetched in a single query rather than issuing separate queries for each association.
99. What is the role of `@Lazy` in Hibernate, and how is it different from `fetch = FetchType.LAZY`?
The `@Lazy` annotation in Hibernate is used to define lazy loading behavior explicitly on a collection or association. It is similar to `fetch = FetchType.LAZY`, but `@Lazy` provides more fine-grained control over how the loading is performed, including specifying whether it should be lazy or eagerly loaded for a specific field or collection.
100. How do you handle `LazyInitializationException` in a Spring-based application when using Hibernate?
To handle `LazyInitializationException`, you can either ensure the session is open when accessing the lazy-loaded association, use `@Transactional` to keep the session open, or initialize the collection manually using `Hibernate.initialize()` before the session is closed.

Set 11

101. How do you configure Hibernate to use `FetchType.LAZY` for a `@OneToMany` relationship?
You can configure `FetchType.LAZY` by annotating the `@OneToMany` relationship with `fetch = FetchType.LAZY`. This ensures that the associated collection is only loaded when accessed, rather than being loaded immediately when the parent entity is retrieved.
102. How does `@OneToOne(fetch = FetchType.EAGER)` behave in Hibernate?
When `@OneToOne(fetch = FetchType.EAGER)` is used, Hibernate loads the associated entity immediately along with the parent entity. This results in a single query fetching both entities, even if the associated entity is not needed in the current context.
103. What is the role of `@Fetch(FetchMode.SELECT)` in Hibernate?
`@Fetch(FetchMode.SELECT)` configures Hibernate to fetch associated entities in separate queries, one for each association. This mode is useful when dealing with large collections and helps avoid the N+1 query problem by issuing optimized queries.
104. What is the difference between `FetchType.LAZY` and `FetchType.EAGER` in Hibernate?
`FetchType.LAZY` defers the loading of the associated entity or collection until it is accessed, while `FetchType.EAGER` loads the associated entity or collection immediately when the parent entity is loaded, which can impact performance when dealing with large datasets.
105. How does Hibernate's `@OneToMany` annotation with `fetch = FetchType.LAZY` help with performance optimization?
`fetch = FetchType.LAZY` helps with performance by deferring the loading of associated collections until they are explicitly accessed, reducing the number of database queries and preventing unnecessary data from being fetched.
106. What are the possible causes of the `LazyInitializationException` in Hibernate?
`LazyInitializationException` occurs when a lazy-loaded association is accessed outside the scope of an open Hibernate session. This typically happens when the session is closed before the associated entity is accessed, or when the entity is detached.
107. How can you prevent the `LazyInitializationException` from occurring in a Spring application?
You can prevent `LazyInitializationException` by using the `@Transactional` annotation, which ensures that the session remains open while the entity is accessed. Alternatively, you can initialize lazy-loaded collections explicitly using `Hibernate.initialize()` before closing the session.
108. What is the impact of using `FetchType.EAGER` in terms of database performance?
Using `FetchType.EAGER` can impact database performance by loading all associated entities or collections immediately, which can lead to large, inefficient queries and unnecessary data retrieval. It is best used when the associated entities are always required.
109. How can you customize the fetch strategy in Hibernate using HQL or JPQL?
You can customize the fetch strategy in HQL or JPQL using `JOIN FETCH` to eagerly fetch associated entities, or by using `LEFT JOIN FETCH` to include associations in the result set while avoiding N+1 query issues. These strategies help control how associations are loaded.

Set 12

110. How can you use `@ManyToMany` with `fetch = FetchType.LAZY` to reduce memory consumption in Hibernate?
By using `fetch = FetchType.LAZY`, Hibernate will only load the related entities in a `@ManyToMany` relationship when they are actually accessed, reducing memory consumption and avoiding unnecessary data retrieval when the associated entities are not required.
111. What happens when you use `fetch = FetchType.EAGER` with `@ManyToOne` in Hibernate?
When you use `fetch = FetchType.EAGER` with `@ManyToOne`, Hibernate will fetch the associated entity immediately when the parent entity is loaded, which can lead to performance issues if the associated entity is not needed in all cases.
112. Can you switch between `FetchType.LAZY` and `FetchType.EAGER` dynamically in Hibernate? How?
You cannot directly switch between `FetchType.LAZY` and `FetchType.EAGER` dynamically using annotations. However, you can control the fetch strategy at runtime by using `Criteria` or `JPQL` with `JOIN FETCH` to customize fetching behavior based on specific use cases.
113. How does Hibernate optimize fetch strategies with `@ManyToOne` and `@OneToMany` relationships?
Hibernate optimizes fetch strategies by deferring the loading of associated entities in `@ManyToOne` (with `FetchType.LAZY`) and `@OneToMany` (with `FetchType.LAZY`), preventing unnecessary queries. Additionally, you can use `@Fetch(FetchMode.JOIN)` to optimize fetch strategies and reduce the number of queries.
114. What is the purpose of `@BatchSize` in Hibernate, and how does it relate to lazy loading?
The `@BatchSize` annotation in Hibernate allows you to specify the number of entities to load in a single batch when fetching associations lazily. This helps optimize performance by reducing the number of database round trips when loading collections or associations.
115. How can you configure lazy loading for collections in Hibernate using `@OneToMany` and `@ManyToOne` associations?
You can configure lazy loading for collections in Hibernate by using `fetch = FetchType.LAZY` in `@OneToMany` and `@ManyToOne` annotations. This ensures that associated collections are only loaded when accessed, reducing the number of queries and improving performance.
116. How can you optimize lazy loading in Hibernate for large collections using `@OneToMany` relationships?
To optimize lazy loading for large collections, you can use `@BatchSize` to load multiple associations in a single batch or configure `@Fetch(FetchMode.SUBSELECT)` to minimize the number of queries. These strategies reduce overhead and optimize performance when working with large datasets.
117. What is the `@LazyCollection` annotation in Hibernate, and how does it help with lazy loading?
The `@LazyCollection` annotation in Hibernate allows you to control the lazy loading behavior of collections, specifying whether the collection should be loaded lazily using a proxy or eagerly. This annotation is useful when you want fine-grained control over the fetching strategy for collections.
118. What are some common mistakes when using lazy loading in Hibernate?
Common mistakes include not managing session scope correctly (leading to `LazyInitializationException`), not using `@Transactional` to keep sessions open, or overusing `FetchType.EAGER` leading to inefficient queries and unnecessary data loading.